﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.UI;
using YJKView;

namespace ColumnModuleAndEdit.ColumnModule
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdImportIrregularColumn : YJKCommandManual
	{
		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			CmdViewType types = (CmdViewType)11;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			Result result2;
			try
			{
				if (cmdData == null)
				{
					throw new System.ArgumentNullException("commandData");
				}
				Document document = cmdData.Application.ActiveUIDocument.Document;
				cmdData.Application.Application.FailuresProcessing += this.OnFailuresProcessing;
				ImportColumnControler importColumnControler = new ImportColumnControler(cmdData);
				string middleFilePath = "";
				Result result = importColumnControler.Import(middleFilePath, ref message);
				if (result != Autodesk.Revit.UI.Result.Succeeded)
				{
					result2 = result;
				}
				else
				{
					result2 = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result2 = Autodesk.Revit.UI.Result.Failed;
			}
			return result2;
		}

		private void OnFailuresProcessing(object sender, FailuresProcessingEventArgs e)
		{
			FailuresAccessor failuresAccessor = e.GetFailuresAccessor();
			string transactionName = failuresAccessor.GetTransactionName();
			IList<FailureMessageAccessor> failureMessages = failuresAccessor.GetFailureMessages();
			if (failureMessages.Count == 0)
			{
				e.SetProcessingResult(0);
				return;
			}
			if (transactionName.Equals("create elements"))
			{
				foreach (FailureMessageAccessor failureMessageAccessor in failureMessages)
				{
					failuresAccessor.ResolveFailure(failureMessageAccessor);
				}
                e.SetProcessingResult((FailureProcessingResult)1);
				return;
			}
            e.SetProcessingResult((FailureProcessingResult)0);
		}
	}
}
